#! /usr/bin/env python

# RSS Parser by Knuthy

#    TarlouzeBot
#    Copyright (C) 2008 Knuthy

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


from BeautifulSoup import BeautifulSoup
from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr
import globalz
import parser
from time import time
from time import asctime
from time import localtime
from threading import *

from globalz import BOT_PASSWORD
from globalz import IRC_CHANNEL
from globalz import IRC_NICK
from globalz import IRC_NICK2
from globalz import IRC_NICK3
from globalz import CHECK_INTERVAL

class Tarlouze(SingleServerIRCBot):
    def __init__(self, channel, nickname, server, port=6667):
        SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
        self.channel = channel
        self.identifiedNick=[]
        self.nicksIndex = 1
        self.sTime = time()

    def setParser (self, tparser):
        self.parser=tparser

    def monitorParser(self):
    	t = Timer (600.0, self.monitorParser)
	t.start()
	if not self.parser.isAlive():
		parser=Parser(CHECK_INTERVAL)
		parser.start()

    def on_nicknameinuse(self, c, e):
        if self.nicksIndex == 1:
            c.nick(IRC_NICK)
            self.nicksIndex = self.nicksIndex + 1
        elif self.nicksIndex == 2:
            c.nick(IRC_NICK2)
            self.nicksIndex = self.nicksIndex + 1
        elif self.nicksIndex == 3:
            c.nick(IRC_NICK3)
            self.nicksIndex = self.nicksIndex + 1
        else:
			return

    def on_welcome(self, c, e):
        c.join(self.channel)

    def on_privmsg(self, c, e):
        self.do_command(e, e.arguments()[0])

    def on_pubmsg(self, c, e):
        a = e.arguments()[0].split(":", 1)
        if len(a) > 1 and irc_lower(a[0]) == irc_lower(self.connection.get_nickname()):
            self.do_command(e, a[1].strip())


    def on_dccmsg(self, c, e):
        c.privmsg("You said: " + e.arguments()[0])

    def on_dccchat(self, c, e):
        if len(e.arguments()) != 2:
            return
        args = e.arguments()[1].split()
        if len(args) == 4:
            try:
                address = ip_numstr_to_quad(args[2])
                port = int(args[3])
            except ValueError:
                return
            self.dcc_connect(address, port)



    def do_command(self, e, cmd):
        nick = nm_to_n(e.source())
        c = self.connection

        if cmd == "disconnect":
            self.disconnect()
        elif cmd == "die":
	    self.parser.active=0
            self.die()
        elif cmd.split()[0] == "identify":
			if len(cmd.split())==1:
				c.notice(nick,"usage: identify <password>")
			elif cmd.split()[1] == BOT_PASSWORD:
				c.notice(nick,"done"); #todo : identification
				self.identifiedNick.append(nick)

			#occupe toi de la partie quand le thread du parser meurt ^^		
        elif cmd.split()[0] == "uptime":
			now = time()
			uptime = now - self.sTime
			
			days = int(uptime/86400)
			uptime = uptime - (days * 86400)
			
			hours = int(uptime/3600)
			uptime = uptime - (hours * 3600)
			
			mins = int(uptime/60)
			uptime = uptime - (mins * 60)
			
			secs = int(uptime)

			c.privmsg (IRC_CHANNEL, str(days)+":"+str(hours)+":"+str(mins)+":"+str(secs))
        else:
			c.notice(nick, "Not understood: " + cmd)

